package com.monkey.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2018/2/1.
 */
@Service
@Transactional
public class MonkeyBoneService {
    private static Logger logger = LoggerFactory.getLogger(MonkeyBoneService.class);
    private static final int ID = 1000;
    private Map<Integer, List<Data>> idsMap = new HashMap<>();

    public void init() {
        // 读取骨骼配置
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = MonkeyBoneService.class.getClassLoader().getResourceAsStream("gou.csv");
            InputStreamReader fr = new InputStreamReader(resourceAsStream);
            BufferedReader buff = new BufferedReader(fr);
            while (buff.ready()) {
                List<Data> list = new ArrayList<>();
                String line = buff.readLine();
                String[] strs = line.split(",");
                for (String s : strs) {
                    if (s.contains("-")) {
                        String[] strs2 = s.split("-");
                        int n = Integer.valueOf(strs2[0]);
                        int m = Integer.valueOf(strs2[1]);
                        list.add(new Data(n, m));
                    } else {
                        int n = Integer.valueOf(s);
                        list.add(new Data(n, n));
                    }
                }
                idsMap.put(list.get(0).low * ID + list.get(1).low, list);
            }
            fr.close();
            buff.close();
        } catch (IOException e) {
            logger.error("read gou.csv error", e);
        } finally {
            if (null != resourceAsStream) {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    logger.error("parse gou.csv error", e);
                }
            }
        }
    }

    public String getUuid(int n, int m) {
        if (0 == n && 0 == m) {
            n = (int) (Math.random() * 6) + 1;
            if (5 == n) {
                n = 24; // 写死
            }
            m = 0;
        }
        List<Data> list = idsMap.get(n * ID + m);
        if (null == list) {
            logger.info("monkey bone get uuid error:{}, {}", n, m);
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (Data d : list) {
            if (d.low == d.high) {
                sb.append(d.low);
            } else {
                sb.append((int) (Math.random() * (d.high - d.low + 1)) + d.low);
            }
            sb.append("-");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public class Data {
        public int low; // 下限
        public int high; // 上限

        public Data(int low, int high) {
            this.low = low;
            this.high = high;
        }
    }
}
